package com.naive.imperative.view;

import android.graphics.Rect;
import android.graphics.RectF;

public class TapArea {
	private RectF frame; // relative to its parent
	private String text;

	public float getWidth(RenderingContext rc) {
		if (frame.width() <= 0) {
			recalculateFrameDimentions(rc);
		}
		return frame.width();
	}

	private void recalculateFrameDimentions(RenderingContext rc) {
		Rect textBounds = new Rect();
		rc.getTextPaint().getTextBounds(text, 0, text.length(), textBounds);

		frame.set(frame.left, frame.top, frame.left + textBounds.width(),
				rc.getTextHeight());
	}

	private void resetFrameDimentions() {
		frame.set(frame.left, frame.top, frame.left, frame.top);
	}

	private RectF getFrame(RenderingContext rc) {
		if (frame.width() <= 0) {
			recalculateFrameDimentions(rc);
		}
		return frame;
	}

	public void setText(String text) {
		this.text = text;
		resetFrameDimentions();
	}

	public void setOrigin(float x, float y) {
		frame.offsetTo(x, y);
	}

	public void draw(RenderingContext rc) {
		final boolean hi = rc.isHighlighted(this);

		rc.getCanvas().drawRoundRect(getFrame(rc),
				3, 3, hi? rc.getBezelPaint() : rc.getBackgroundPaint());
		rc.getCanvas().drawText(text, frame.left, frame.bottom, rc.getTextPaint());
	}
}
